package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
    @Override
    public PageResult findParent(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page all = menuDao.findParent(queryString);
        List result = all.getResult();
        long total = all.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public List<Menu> findChildrenById(Integer parentId) {
        return menuDao.findChildrenById(parentId);
    }

    @Override
    public Menu findById(Integer menuId) {
        return menuDao.findById(menuId);
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        long countRole = menuDao.findRoleCountByMenu(id);
        List<Menu> childrenById = menuDao.findChildrenById(id);
        if (countRole > 0) {
            throw new RuntimeException("当前菜单被"+countRole+"个角色引用,不能删除");
        }
        if (childrenById.size() > 0 ||childrenById==null) {
            throw new RuntimeException("当前菜单有"+childrenById.size()+"个子角色,不能删除");
        }

        menuDao.deleteById(id);
    }

    @Override
    public List<Map> findMenuByUser(String username) {
        List<Menu> menus = menuDao.findParentMenuByUsername(username);
        List<Map> list = new ArrayList<>();
        if (menus != null && menus.size() > 0){
            for (Menu menu : menus) {
                    Map parentMap = new HashMap();
                    parentMap.put("path",menu.getPath());
                    parentMap.put("title",menu.getName());
                    parentMap.put("icon",menu.getIcon());
                    parentMap.put("children",findChildrenMenubByUsername(username,menu.getId()));
                    list.add(parentMap);

            }
        }
        return list;
    }

    private List<Map> findChildrenMenubByUsername(String username, Integer parentId) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("username",username);
        map.put("parentId",parentId);
        List<Menu> childMenus = menuDao.findChildrenMenubByUsername(map);
        List<Map> list= new ArrayList<>();
        for (Menu childMenu : childMenus) {
            Map childMap = new HashMap();
            childMap.put("path",childMenu.getPath());
            childMap.put("title",childMenu.getName());
            childMap.put("linkUrl",childMenu.getLinkUrl());
            list.add(childMap);
        }
        return list;

    }


    public void setMenuAndPermission(Integer menuId, Integer[] permissionIds) {
        if (permissionIds!=null&&permissionIds.length>0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("menuId", menuId);
                map.put("permissionId", permissionId);
                menuDao.setMenuAndPermission(map);
            }
        }
    }



    @Override
    public List<Integer> findPermissionIdsByMenuId(Integer menuId) {
        return menuDao.findPermissionIdsByMenuId(menuId);
    }
    
    
    







}
